﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using Sim;
using Lists;
using Users;

namespace SimTest
{
    [TestFixture]
    public class TestClass
    {
        [Test]
        public void CreateUserListTest()
        {
            UserList.SerializeToXML();
            UserList.AddUser(new User("Van", "Nguyen", 21, "VN", "Male", 1, "vanngh121", "hejhopp123"));
            UserList.AddUser(new User("Amjad", "Hematkar", 24, "IR", "Male", 0, "amjmah121", "lolol"));
            UserList.AddUser(new User("Gabriel", "Kasto", 23, "SY", "Male", 0, "gabkah121", "yoyo"));
            UserList.AddUser(new User("Adam", "Ludenberg", 22, "SE", "Female", 0, "adaluh121", "derp"));

            StringAssert.AreEqualIgnoringCase("Van" + "Nguyen", (UserList.userList[0].firstName) + (UserList.userList[0].lastName));
            StringAssert.AreNotEqualIgnoringCase("Amjad" + "Kalle", (UserList.userList[1].firstName) + (UserList.userList[1].lastName));
            UserList.userList.Clear();
            UserList.SerializeToXML();
            
        }
        [Test]
        public void CreateTourneyTest()
        {
            TournamentList.AddTournament(new Tournament("VM14", DateTime.Today, 10, "Male", 5, 3));

            StringAssert.AreEqualIgnoringCase("VM14" + "2014-02-25 00:00:00", (TournamentList.tournamentList[0].title + TournamentList.tournamentList[0].date));
            StringAssert.AreNotEqualIgnoringCase("VM300" + "-----", (TournamentList.tournamentList[0].title + TournamentList.tournamentList[0].date));

            TournamentList.tournamentList.Clear();
            TournamentList.SerializeToXML();
        }

        [Test]
        public void AddDiveTest()
        {
            DDList dd = new DDList();
            
            TournamentList.SerializeToXML();
            Tournament tour1 = new Tournament("VM66666", DateTime.Today, 10, "Male", 5, 3);
            TournamentList.AddTournament(tour1);
            
            UserList.AddUser(new User("Van", "Nguyen", 21, "VN", "Male", 0, "vanngh121", "hejhopp123"));

            tour1.listOfDivers.AddDiver(new Diver("Van", "Nguyen", 21, "VN", "Male", 0, "vanngh121", "hejhopp123"));


            tour1.listOfDivers.diverList[0].dives.Add(new Dive());
            tour1.listOfDivers.diverList[0].dives[0].setJudgeScore(4.5, 1);
            tour1.listOfDivers.diverList[0].dives[0].setJudgeScore(3.5, 2);
            tour1.listOfDivers.diverList[0].dives[0].setJudgeScore(5.0, 3);
            tour1.listOfDivers.diverList[0].dives[0].setJudgeScore(5.5, 4);
            tour1.listOfDivers.diverList[0].dives[0].setJudgeScore(7.0, 5);


            tour1.listOfDivers.diverList[0].dives[0].CalcPtsnDD("100A", tour1.jumpHeight);
            TournamentList.AddTournament(tour1);
            StringAssert.AreEqualIgnoringCase("100A", (tour1.listOfDivers.diverList[0].dives[0].diveCode));
            StringAssert.AreNotEqualIgnoringCase("200A", (tour1.listOfDivers.diverList[0].dives[0].diveCode));
            Assert.AreEqual(5, tour1.listOfDivers.diverList[0].dives[0].diveScore.Count);

            Assert.AreEqual(15.0, (tour1.listOfDivers.diverList[0].dives[0].calculatedScore));
            Assert.AreNotEqual(150000.0, (tour1.listOfDivers.diverList[0].dives[0].calculatedScore));

            TournamentList.tournamentList.Clear();
            TournamentList.SerializeToXML();


            UserList.userList.Clear();
            UserList.SerializeToXML();
        }

        [Test]
        public void LoginTest()
        {

            Login l = new Login();

            UserList.SerializeToXML();
            UserList.AddUser(new User("gobi", "gogoo", 15, "RU", "yes", 1, "gabkah121", "hejhej"));
            UserList.AddUser(new User("gobii", "gogooo", 15, "RU", "yes", 1, "gabkah1211", "hejhej"));
            UserList.AddUser(new User("gobiii", "gogoooo", 15, "RU", "yes", 1, "gabkah12111", "hejhej"));
            UserList.AddUser(new User("gobiiii", "gogooooo", 15, "RU", "yes", 1, "gabkah121111", "hejhej"));
            UserList.AddUser(new User("gobiiiii", "gogoooooo", 15, "RU", "yes", 1, "gabkah1211111", "hejhej"));
            UserList.AddUser(new User("Amjed", "MA", 15, "IR", "yes", 1, "amji", "looool"));
            UserList.AddUser(new User("Amjed", "MA", 15, "IR", "yes", 1, "amjid", "looool"));
            UserList.AddUser(new User("Amjed", "MA", 15, "IR", "yes", 1, "amjad", "looool"));
            UserList.AddUser(new User("Amjed", "MA", 15, "IR", "yes", 1, "amjii", "hej"));
            UserList.DeserializeFromXML();

            Assert.AreEqual(1, (l.Validate("amji", "looool")));
            Assert.AreEqual(0, (l.Validate("amjsdfi", "loosdfool")));
            Assert.AreNotEqual(1, (l.Validate("gabkah121", "looool")));
            Assert.AreNotEqual(1, (l.Validate("gabkah121111", "hej")));

            UserList.userList.Clear();
            UserList.SerializeToXML();
        }

        [Test]
        public void TestDiverList()
        {
            var diverList_ = new DiverList();
            Assert.AreEqual(0, diverList_.diverList.Count);
            UserList.AddUser(new User("Van", "Nguyen", 21, "VN", "Male", 0, "vanngh121", "hejhopp123"));
            diverList_.AddDiver(new Diver("Van", "Nguyen", 21, "VN", "Male", 0, "vanngh121", "hejhopp123"));
            Assert.AreEqual(1, diverList_.diverList.Count);
            Assert.AreNotEqual(2, diverList_.diverList.Count);

            UserList.userList.Clear();
            UserList.SerializeToXML();
        }

        [Test]
        public void TestJudgeList()
        {
           
            var judgeList_ = new JudgeList();
            Assert.AreEqual(0, judgeList_.judgeList.Count);
            UserList.AddUser(new User("Van", "Nguyen", 21, "VN", "Male", 1, "vanngh121", "hejhopp123"));
            judgeList_.AddJudge((new Judge("Van", "Nguyen", 21, "VN", "Male", 1, "vanngh121", "hejhopp123")));
            Assert.AreEqual(1, judgeList_.judgeList.Count);
            Assert.AreNotEqual(2, judgeList_.judgeList.Count);

            UserList.userList.Clear();
            UserList.SerializeToXML();
        }

        [Test]
        public void TestScoreList()
        {
            var scoreList_ = new ScoreList();
            Assert.AreEqual(0 , scoreList_.scoreList.Count);
            scoreList_.scoreList.Add(new Score(4.5, 1));
            Assert.AreEqual(1, scoreList_.scoreList.Count);
            Assert.AreNotEqual(2, scoreList_.scoreList.Count);
        }

        [Test]
        public void TestTournamentList()
        {
            
            Assert.AreEqual(0, TournamentList.tournamentList.Count);
            TournamentList.AddTournament(new Tournament("VM14", DateTime.Today, 10, "Male", 5, 3));
            Assert.AreEqual(1, TournamentList.tournamentList.Count);
            Assert.AreNotEqual(2, TournamentList.tournamentList.Count);
            TournamentList.tournamentList.Clear();
            TournamentList.SerializeToXML();
        }

        [Test]
        public void TestEmptyUserList()
        {
            UserList.userList.Clear();
            Assert.AreEqual(0, UserList.userList.Count);
            UserList.AddUser(new User("gobi", "gogoo", 15, "RU", "yes", 1, "gabkah121", "hejhej"));
            Assert.AreEqual(1, UserList.userList.Count);
            Assert.AreNotEqual(2, UserList.userList.Count);

            UserList.userList.Clear();
            UserList.SerializeToXML();
        }

        [Test]
        public void TestAddAndRemove()
        {
            
            var scoreList_ = new ScoreList();
            UserList.SerializeToXML();
            TournamentList.SerializeToXML();
            TournamentList.AddTournament(new Tournament("VM66666", DateTime.Today, 10, "Male", 5, 3));
            TournamentList.AddTournament(new Tournament("VM104", DateTime.Today, 7, "Male", 5, 3));

            UserList.AddUser(new User("gobi", "gogoo", 15, "RU", "yes", 0, "gabkah121", "hejhej"));
            UserList.AddUser(new User("gobi", "gogoo", 15, "RU", "yes", 0, "gabkah1211", "hejhej1"));
            UserList.AddUser(new User("gobi", "gogoo", 15, "RU", "yes", 1, "gabkah12111", "hejhej"));
            UserList.AddUser(new User("gobi", "gogoo", 15, "RU", "yes", 1, "gabkah121111", "hejhej1"));

            TournamentList.tournamentList[0].listOfDivers.AddDiver(new Diver("gobi", "gogoo", 15, "RU", "yes", 0, "gabkah121", "hejhej"));
            TournamentList.tournamentList[0].listOfDivers.AddDiver(new Diver("gobi", "gogoo", 15, "RU", "yes", 0, "gabkah1211", "hejhej1"));

            TournamentList.tournamentList[0].listOfJudges.AddJudge(new Judge("gobi", "gogoo", 15, "RU", "yes", 1, "gabkah12111", "hejhej"));
            TournamentList.tournamentList[0].listOfJudges.AddJudge(new Judge("gobi", "gogoo", 15, "RU", "yes", 1, "gabkah121111", "hejhej1"));

            Assert.AreEqual(2, TournamentList.tournamentList[0].listOfJudges.judgeList.Count);
            TournamentList.tournamentList[0].listOfJudges.RemoveJudge(0);
            Assert.AreEqual(1, TournamentList.tournamentList[0].listOfJudges.judgeList.Count);
            Assert.AreEqual("gabkah121111", TournamentList.tournamentList[0].listOfJudges.judgeList[0].username);

            Assert.AreEqual(2, TournamentList.tournamentList[0].listOfDivers.diverList.Count);
            TournamentList.tournamentList[0].listOfDivers.RemoveDiver(0);
            Assert.AreEqual(1, TournamentList.tournamentList[0].listOfDivers.diverList.Count);
            Assert.AreEqual("gabkah1211", TournamentList.tournamentList[0].listOfDivers.diverList[0].username);

            Assert.AreEqual(4, UserList.userList.Count);
            UserList.RemoveUser(0);
            UserList.RemoveUser(0);
            UserList.RemoveUser(0);
            Assert.AreEqual(1, UserList.userList.Count);
            Assert.AreEqual("gabkah121111", UserList.userList[0].username);

            Assert.AreEqual(2, TournamentList.tournamentList.Count);
            TournamentList.RemoveTournament(0);
            Assert.AreEqual(1, TournamentList.tournamentList.Count);
            Assert.AreEqual("VM104", TournamentList.tournamentList[0].title);

            UserList.userList.Clear();
            UserList.SerializeToXML();

            TournamentList.tournamentList.Clear();
            TournamentList.SerializeToXML();
        }

        [Test]
        public void TestScoreConstructor()
        {
            Score s = new Score();

            Assert.AreEqual(0.0, s.scoreOfJump);
            Assert.AreEqual(0, s.idOfJudge);
        }

        [Test]
        public void TestTournamentConstructor()
        {
            Tournament t = new Tournament();

            Assert.AreEqual(false, t.isStarted);
            Assert.AreEqual("", t.title);
            Assert.AreEqual(DateTime.Today, t.date);
            Assert.AreEqual(0, t.jumpHeight);
            Assert.AreEqual("", t.sex);
            Assert.AreEqual(0, t.totalJudges);
            Assert.AreEqual(0, t.totalDivers);
            Assert.AreEqual(0, t.listOfDivers.diverList.Count);
            Assert.AreEqual(0, t.listOfJudges.judgeList.Count);

            t = new Tournament("VM104", DateTime.Today, 7, "Male", 5, 3);


            Assert.AreEqual("VM104", t.title);
            Assert.AreEqual(7, t.jumpHeight);
            Assert.AreEqual("Male", t.sex);
            Assert.AreEqual(5, t.totalJudges);
            Assert.AreEqual(3, t.totalDivers);
        }

        [Test]
        public void TestDiveConstructor()
        {
            Dive d = new Dive();

            Assert.AreEqual("", d.diveCode);
            Assert.AreEqual(0.0, d.diveDifficulty);
            Assert.AreEqual(0, d.diveHeight);
            Assert.AreEqual(0, d.calculatedScore);
            Assert.AreEqual(0, d.diveScore.Count);
        }
    }
}

